package com.gxkj.projects.myshopx.controllers.admin;

import com.gxkj.common.exceptions.ValidateException;
import com.gxkj.common.utils.ListPager;
import com.gxkj.projects.myshopx.dto.AttributeDto;
import com.gxkj.projects.myshopx.dto.ReturnData;
import com.gxkj.projects.myshopx.entitys.Attribute;
import com.gxkj.projects.myshopx.entitys.GoodType;
import com.gxkj.projects.myshopx.enums.ErrorCodeEnum;
import com.gxkj.projects.myshopx.enums.GoodTypeStates;
import com.gxkj.projects.myshopx.services.AttributeService;
import com.gxkj.projects.myshopx.services.GoodsTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

import static com.gxkj.projects.myshopx.util.SessionUserUtil.getUser;

@Controller
@RequestMapping("/admin/attribute")
public class AdminAttributeController {

    private static final Logger LOG = LoggerFactory.getLogger(AdminAttributeController.class);

    @Autowired
    private AttributeService attributeService;

    @Autowired
    private GoodsTypeService goodsTypeService;

    @RequestMapping(value="")
    public String index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
        String mv = "admin/admin_attribute";
        return mv;
    }

    @RequestMapping(value="dopage",method= RequestMethod.GET)
    @ResponseBody
    public ReturnData<ListPager<AttributeDto>> dopage(Attribute entity,
                                               int pageno, int pagesize,
                                               HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {

        ReturnData<ListPager<AttributeDto>> returnData = new ReturnData<ListPager<AttributeDto>>();
        ListPager<Attribute> pager =  attributeService.doPageHQL(pageno,pagesize,entity);

        List<AttributeDto> listAttributeDto = new ArrayList<AttributeDto>();
        List<Attribute> pagerList =   pager.getPageData();

        ListPager<AttributeDto> dtopager = new ListPager();
        dtopager.setRowsPerPage(pager.getRowsPerPage());
        dtopager.setPageNo(pager.getPageNo());
        dtopager.setTotalRows(pager.getTotalRows());
        if(pagerList != null && pagerList.size()>0){
            for(Attribute attribute : pagerList){
                AttributeDto dto = new AttributeDto();
                BeanUtils.copyProperties(attribute,dto);

                GoodType goodType = goodsTypeService.getById(attribute.getTypeId());
                if(goodType!= null){
                    dto.setTypeIdText(goodType.getName());
                }
                listAttributeDto.add(dto);
            }
            dtopager.setPageData(listAttributeDto);
        }
        returnData.setEntity(dtopager);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;

    }
    @RequestMapping(value="doadd",method= RequestMethod.POST)
    @ResponseBody
    public  ReturnData<Attribute>doAdd(Attribute entity,
                                   HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData<Attribute> returnData = new ReturnData<Attribute>();
        attributeService.doAddAttribute(getUser(request),entity);
        returnData.setEntity(entity);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }

    @RequestMapping(value="get",method= RequestMethod.GET)
    @ResponseBody
    public  ReturnData<Attribute>get(String id,
                                 HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData<Attribute> returnData = new ReturnData<Attribute>();
        Attribute entity = attributeService.getById(id);
        returnData.setEntity(entity);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }

    @RequestMapping(value="doupdate",method= RequestMethod.POST)
    @ResponseBody
    public  ReturnData<Attribute>doupdate(Attribute entity ,
                                      HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData<Attribute> returnData = new ReturnData<Attribute>();
        attributeService.doUpdateAttribute(getUser(request),entity);
        returnData.setEntity(entity);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }
    @RequestMapping(value="dodel",method= RequestMethod.POST)
    @ResponseBody
    public  ReturnData<Boolean>dodel(String id,
                                     HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws ValidateException {

        ReturnData<Boolean> returnData = new ReturnData<Boolean>();
        attributeService.doDeleteAttribute(getUser(request),id);
        returnData.setEntity(true);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;
    }
    @RequestMapping(value="/goodstype/dopage",method= RequestMethod.GET)
    @ResponseBody
    public ReturnData<ListPager<GoodType>> goodstypeDopage(

                                                  HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){

        ReturnData<ListPager<GoodType>> returnData = new ReturnData<ListPager<GoodType>>();
        GoodType entity = new GoodType();
        entity.setStates(GoodTypeStates.ENABLE);
        int pageno = 0 ;
        int pagesize = Integer.MAX_VALUE;
        ListPager<GoodType> pager =  goodsTypeService.doPageHQL(pageno,pagesize,entity);
        returnData.setEntity(pager);
        returnData.setStatusCode(ErrorCodeEnum.NORMAL.getCode());
        return returnData;

    }
}
